var buttons;
var t_time;
var from_id = 0;
$("document").ready(function() {
	load_tweets();
	load_ranks();	
	hello = setInterval(load_tweets, 20000);
	//setInterval('updateClock()', 1000 )
	$('#command').keyup(function(event) {
  		len_body = $(this).val().length - 7;
  		if (len_body > 0 ) {
  			$('#cmt_like').text("Total Words:" + len_body);
  			$('#cmt_dislike').hide();
  		} else {
  			$('#cmt_like').text("Like");
  			$('#cmt_dislike').show();
		} 
	});
});
function reply_vote(vote,iid){
        $.post("post.php", { vote : vote, iid : iid});
}
function submit_tweet(iid){
       	var txt_area = "textarea#text" + iid; 
		var text = $(txt_area).val();	
		$.post("posttweet.php",{ text_content : text ,iid :iid });
		$.post("post.php", { vote: 1, iid : iid});	
		hello = setInterval(load_tweets, 1500);
        document.getElementById(iid).style.display = 'none';
}

//function show_textbox(){
//	$("#resultmessage").html("<div style='display:block; background-color: Green; color: White; font-size: 15px; font-weight: bold; padding: 0px 0px 0pxh0px;'>Saved!")
//	$("#resultmessage").show('slow');
//}
function load_txtbx(id) {
		document.getElementById('command').value = ">>" + id + " ";
		$('#cmt_comment').text('Submit');
		$('#info-ranking').text('Close');
		$('#commandbox').fadeIn('fast', function() {});
		$('#cmt_like').fadeIn('slow', function() {});
		$('#cmt_dislike').fadeIn('slow', function() {});
		$('#cmt_comment').fadeIn('slow', function() {});
		document.getElementById('command').focus();
}
function toggle_stream() {
	if ($('#info-ranking').text() == 'Ladder') {
		$('#info-ranking').text('Stream');
		$('#twitter_results').fadeOut('slow', function() {});
		$('#twitterblock').fadeOut('slow', function() {});
		$('#twitter_ranks').fadeIn('slow', function() {});
	} else if ($('#info-ranking').text() == 'Stream') {
		$('#info-ranking').text('Ladder');
		$('#twitter_ranks').fadeOut('slow', function() {});
		$('#twitter_results').fadeIn('slow', function() {});
		$('#twitterblock').fadeIn('slow', function() {});
	} else if ($('#info-ranking').text() == 'Close Header') {
		$('#headblock').slideUp('fast', function() {});
		$('#info-ranking').text('Show Header');
	} else if ($('#info-ranking').text() == 'Show Header') {
		$('#headblock').slideDown('fast', function() {});
		$('#info-ranking').text('Close Header');
	} else if ($('#info-ranking').text() == 'Close') {
		if ($('#twitter_blog').text() == 'People @ Twitter') {
			$('#info-ranking').text('Close Header');
		} else {
			$('#info-ranking').text('Ladder');
		}
		$('#commandbox').fadeOut('slow', function() {});
		$('#cmt_like').fadeOut('slow', function() {});
		$('#cmt_dislike').fadeOut('slow', function() {});
		$('#cmt_comment').text('Say Something');
	}
}

function toggle_content() {
	if ($('#twitter_blog').text() == 'People @ Twitter') {
		$('#info-ranking').text('Ladder');
		$('#twitter_blog').text('Official Blog of the Site');
		$('#headblock').fadeOut('fast', function() {});
		$('#blogblock').fadeOut('fast', function() {});
		$('#twitter_results').fadeIn('fast', function() {});
		$('#twitterblock').fadeIn('fast', function() {});
	} else if ($('#twitter_blog').text() == 'Official Blog of the Site') {
		$('#twitter_blog').text('People @ Twitter');
		$('#info-ranking').text('Close Header');
		$('#twitter_ranks').fadeOut('fast', function() {});
		$('#headblock').fadeIn('fast', function() {});
		$('#blogblock').fadeIn('fast', function() {});
		$('#twitterblock').fadeOut('fast', function() {});
		$('#twitter_results').fadeOut('fast', function() {});
	}
}
function load_ranks() {
	var rank_result = '';
	$.getJSON('/json/ranking.json?lang=en', function(data) {
		var i = 0;
		for ( i = 0; i <= 9; i++) {
			n = 9 - i
			var title = data[i].title;
			var id = data[i].id;
			var rank = data[i].rank;
			var time = data[i].time;
			rank_result = rank_result + "<li><a href='javascript:load_txtbx("+ id +")' title=" + id + " style = 'color:#F3" + i + "'>" + title  + "<br><em>Rank: " + rank + " " +  time + "</em></a></br></li>";			
		}
		//$("#twitter_ranks").remove();
		$("#twitter_ranks ul").prepend(rank_result);
		$("#twitter_ranks ul").fadeIn("slow");	
	});
}

function load_tweets() {
	var result = '';	
	$.getJSON('/json/info.json?since=' + from_id + '&lang=en', function(data) {
	         var new_tweet = data[0].new_tweet;
	         if ( new_tweet > 0 ) {
			from_id = data[1].id	
			var i = 0;
			for (i = 1; i <= new_tweet; i++) {	
				var title = data[i].title;
				var id = data[i].id;	
				var rank = data[i].rank;
				var time = data[i].time;
				result = result +  "<li><a href='javascript:load_txtbx(" + id + ")' title=" + id + ">" + title  + "<br><em>Rank: " + rank + " " +  time + "</em></a></br></li>" 
				
			}
			$("#twitter_results ul").prepend(result);
			$("#twitter_results ul").fadeIn("slow");	 
		} else {
	             result = "There are no new posts now."
	        }
	});	
}
function show_textbox() {
	document.getElementById('command').value = ""
	$('#cmt_comment').text('Submit');
	$('#info-ranking').text('Close');
	$('#commandbox').fadeIn('slow', function() {});
    $('#cmt_comment').fadeIn('slow', function() {});
	document.getElementById('command').focus();
}

function linkify(content){
    // modified from TwitterGitter by David Walsh (davidwalsh.name)
    // courtesy of Jeremy Parrish (rrish.org)
    return content.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
               .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
               .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
}
